package cn.hinglo.common.interceptors.exception;


import cn.hinglo.common.config.error.SelfErrorRender;
import cn.hinglo.common.enums.ResponseEnum;
import cn.hinglo.common.exception.ResultException;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import lombok.extern.slf4j.Slf4j;

/**
 * @createTime: 2018/2/11
 * @author: HingLo
 * @description: 全局异常拦截器处理
 */
@Slf4j
public class ExceptionInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();
        try {
            inv.invoke();
        } catch (ActiveRecordException e) {
            SelfErrorRender.errorRender(ResponseEnum.DATABASEERROR, controller, e);
        } catch (cn.hutool.core.date.DateException e) {
            SelfErrorRender.errorRender(ResponseEnum.ORTHERERROR.getCode(), "指定日期格式不正确,请联系管理员", controller, e);
        } catch (ResultException e) {
            SelfErrorRender.errorRender(e.getCode(), e.getMessage(), controller, e);
        } catch (Exception e) {
            SelfErrorRender.errorRender(ResponseEnum.ORTHERERROR.getCode(), e.getMessage(), controller, e);
        }
    }

}
